from course.models import CourseBanner,Course,Video,Lesson,Resource
from users.models import GradeStudent
from rest_framework import serializers
from users.serializer import UserInfoSerializer,GradeInfoSerializer
from .models import HotSearchWords


class GoodsImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseBanner
        fields = ("course","image")


class CourseCreateSerializer(serializers.ModelSerializer):
    teacher = UserInfoSerializer(read_only=True)
    grade = GradeInfoSerializer(read_only=True)
    is_login = serializers.BooleanField(default=False,read_only=True)
    is_class = serializers.BooleanField(default=False, read_only=True)

    class Meta:
        model = Course
        fields = "__all__"


class CourseSummarySerializer(serializers.ModelSerializer):
    teacher = serializers.StringRelatedField()
    students = serializers.IntegerField(source="grade.GradeStudent.count")

    class Meta:
        model = Course
        fields = ("id","name","image","tag","teacher","students")


class CourseDetailSerializer(serializers.ModelSerializer):
    teacher = UserInfoSerializer()
    grade = GradeInfoSerializer()
    is_login = serializers.BooleanField(default=False,read_only=True)
    is_class = serializers.BooleanField(default=False, read_only=True)

    class Meta:
        model = Course
        fields = "__all__"


class HotWordsSerializer(serializers.ModelSerializer):
    class Meta:
        model = HotSearchWords
        fields = ("keywords",)


class VideoSerializer(serializers.ModelSerializer):

    class Meta:
        model = Video
        fields = ("id","name","learn_times")


class ResourceSerializer(serializers.ModelSerializer):

    class Meta:
        model = Resource
        fields = ("id","name","downloads")


class LessonSerializer(serializers.ModelSerializer):
    videos = VideoSerializer(read_only=True,many=True)

    class Meta:
        model = Lesson
        fields = ("id","name","videos")


class CourseAuthDetailSerializer(serializers.ModelSerializer):
    teacher = UserInfoSerializer()
    grade = GradeInfoSerializer()
    is_login = serializers.BooleanField(default=True,read_only=True)
    is_class = serializers.BooleanField(default=False, read_only=True)
    students = serializers.IntegerField(source="grade.GradeStudent.count")

    class Meta:
        model = Course
        fields = "__all__"


class CourseClassAuthDetailSerializer(serializers.ModelSerializer):
    teacher = UserInfoSerializer()
    grade = GradeInfoSerializer()
    lessons = LessonSerializer(read_only=True,many=True)
    is_login = serializers.BooleanField(default=True,read_only=True)
    resource = ResourceSerializer(read_only=True,many=True)
    is_class = serializers.BooleanField(default=True, read_only=True)
    students = serializers.IntegerField(source="grade.GradeStudent.count")

    class Meta:
        model = Course
        fields = "__all__"


class LessonDetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = Lesson
        fields = ("id","name","course")


class VideoDetailSerializer(serializers.ModelSerializer):
    lesson = LessonDetailSerializer()

    class Meta:
        model = Video
        fields = ("id","name","learn_times","lesson","url")


